package com.sdutcm.Controller;

import com.sdutcm.Domain.Lend;
import com.sdutcm.Service.ServiceImpl.LendServiceImpl;
import com.sdutcm.Util.TimeUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.websocket.server.PathParam;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 *@Author Yan
 *@Create 2019-08-20 19:10
 */
@Controller
public class LendController {

    @Autowired
    private LendServiceImpl lendService;
    // 添加借出记录
    @RequiresRoles({"user"})
    @PostMapping("/lendAdd")
    public String lendAdd(@PathParam("l_person") String l_person,
                          @PathParam("l_manager") String l_manager,
                          @PathParam("l_label") String l_label) throws Exception {
        Lend lend = new Lend();
        lend.setL_person(l_person);
        lend.setL_manager(l_manager);
        lend.setL_label(l_label);
        lendService.lendAdd(lend);
        return "redirect:/lendList";
    }

    // 查看当前所有
    @GetMapping("/lendList")
    public ModelAndView lendList() throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("lendLists",lendService.lendList());
        mv.setViewName("lend-list");
        return mv;
    }

    // 查看历史所有
    @GetMapping("/lendListHis")
    public ModelAndView lendListHis() throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("lendLists",lendService.lendListHis());
        mv.setViewName("lend-list-his");
        return mv;
    }

    // 归还时间添加
    @GetMapping("/listEdit/{id}")
    public ModelAndView listEdit(@PathVariable("id")Integer id) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("lendList",lendService.listEdit(id));
        mv.setViewName("lend-Edit");
        return mv;
    }


    @Autowired
    private TimeUtils timeUtils;

    // 修改状态
    @PutMapping("/listEdit")
    public String lendEditTime(@PathParam("l_id") Integer l_id,
                               @PathParam("l_label") String l_label) throws Exception {
        Lend lend = new Lend();

        lend.setL_label(l_label);
        lend.setL_id(l_id);
        lendService.lendEditTime(lend);
        return "redirect:/lendListHis";
    }
}
